<?php
	require_once 'SQLHelper.class.php';

	class SignService{

		private $time_start = ' 05:00:00';
		private $time_end = ' 09:03:00';
		private $today_end = ' 23:59:59';
		private $num_top = 7;

		public function signIn($username, $comment){
			$sqlHelper = new SQLHelper();
			$now = date('y-m-d H:i:s');
			$today = date('y-m-d');
			$weekday = date('w');

			$endTime = $today.$this->time_end;
			$startTime = $today.$this->time_start;

			if(strtotime($startTime) >= strtotime($now))
			{
				return -3;
			}
			if($this->checkIsSign($username))
			{
				return -2;
			}
			//file_put_contents("d:/mylog.log",'endTime: '.$endTime.', now:'.$now."\r\n",FILE_APPEND);
			//如果大于9点十五，则不允许再签到
			if(strtotime($endTime) <= strtotime($now))
			{
				// 周六周日奖励签到
				if($weekday == 0 || $weekday == 6)
				{
					$comment = $comment . "[周末奖励签到]";
					$sql = "insert into signlog (userid,signTime,comment, times) values('$username',now(),'$comment', 1)";
					return $sqlHelper->exc_dml($sql);
				} else {
					$sql = "insert into signlog (userid,signTime,comment, times) values('$username',now(),'$comment', 0)";
					$sqlHelper->exc_dml($sql);
					return -1;
				}
				
			}
			$sql = "insert into signlog (userid,signTime,comment, times) values('$username',now(),'$comment', 1)";
			return $sqlHelper->exc_dml($sql);
		}
				
		public function getSignTimes($username, $begin, $end){
			$sqlHelper = new SQLHelper();
			$sql = "select DAYOFMONTH(signTime) dayOfMonth, times isLate from signlog where userid='$username' and signTime >'$begin' and signTime < '$end'";
			$arr = $sqlHelper->exc_dql2($sql);
			$sqlHelper->connect_close();
			$res_info = json_encode($arr);
			//file_put_contents("d:/mylog.log",'sql:'.$sql."\r\n",FILE_APPEND);
			return $res_info;
		}

		public function checkIsSign($username){
			$sqlHelper = new SQLHelper();
			$today = date('y-m-d');
			$startTime = $today.$this->time_start;
			$endTime = $today.' 23:59:59';
			$sql = "select userid from signlog where userid='$username' and signTime > '$startTime' and signTime < '$endTime'";
			$arr = $sqlHelper->exc_dql2($sql);
			$sqlHelper->connect_close();
			if(count($arr) > 0)
				return true;
			return false;
		}

		public function getSignedList(){
			$sqlHelper = new SQLHelper();
			$today = date('y-m-d');
			$startTime = $today.$this->time_start;
			$endTime = $today.$this->today_end;

			$sql = "select userid,signTime,comment,times from signlog where signTime > '$startTime' and signTime < '$endTime'";

			//file_put_contents("d:/mylog.log",$sql."\r\n",FILE_APPEND);

			$arr = $sqlHelper->exc_dql2($sql);
			$sqlHelper->connect_close();
			
			//返回消息格式
			/*
			$res_info = "[";
			for($i=0; $i<count($arr); $i++){
				$row = $arr[$i];
				if($i == (count($arr)-1)){
					$res_info.='{"username":"'.$row['username'].'"}]';
				}else{
					$res_info.='{"username":"'.$row['username'].'"},';
				}
			}
			*/
			$res_info = json_encode($arr);
			//file_put_contents("d:/mylog.log",$res_info."\r\n",FILE_APPEND);
			return $res_info;
		}

		// 获取每月迟到top5
		public function getTopLate(){
			$top = $this->num_top;
			$sqlHelper = new SQLHelper();
			$today = date('y-m');
			$startTime = $today.'-01';
			$endTime = $today.'-31';

			$sql = "select count(userid) times, userid from signlog where signTime > '$startTime' and signTime < '$endTime' and times = 0 group by userid order by times desc limit $top ";

			$arr = $sqlHelper->exc_dql2($sql);
			$sqlHelper->connect_close();
			
			$res_info = json_encode($arr);
			return $res_info;
		}
		
	}
